//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
// 二维码
var QR = require("../../common/qrcode.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    inviteCode: "",
    pageData: [],
    src: "",
    // 二维码标识
    url: "",
    // 
    uid:"",
    userType:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.setData({
      url: "http://www.eee2016.net/BarterBusiness/Trans/TransForm.aspx?uid=" + this.data.uid
    })
    this.GetInvitationImg();
    //动态设置画布大小
    var size = this.setCanvasSize();
    this.createCodeImg();
    this.GetUserInfoByID();
  },

  
  //  GetInvitationImg
  GetInvitationImg: function() {
    var that = this;
    var pageData = this.data.pageData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetInvitationImg"),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        pageData = json.ResultData.split(";");
        that.setData({
          pageData: pageData,
          src: pageData[0],
        })
      }
    })
  },
  //  GetUserInfoByID
  GetUserInfoByID: function() {
    var that = this;
    var uid = this.data.uid;
    var inviteCode = this.data.inviteCode;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByID&UID=" + uid),
      success: function(res) {
        inviteCode = JSON.parse(baseJs.decryUrl(res.data)).ResultData.UniqueNumber;
        that.setData({
          inviteCode: inviteCode
        })
      }
    })
  },
  // changeImg更换邀请图片
  changeImg: function() {
    var that = this;
    var pageData = this.data.pageData;
    var src = this.data.src;
    if (src == pageData[0]) {
      this.setData({
        src: pageData[1]
      })
    }else{
      this.setData({
        src: pageData[0]
      })
    }

  },
  // 二维码
  //适配不同屏幕大小的canvas
  setCanvasSize: function() {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 300; //不同屏幕下canvas的适配比例；设计稿是750宽
      //canvas画布为正方形
      var width = res.windowWidth / scale;
      var height = width;
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function() {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function(res) {
        var tempFilePath = res.tempFilePath;
        // console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function(res) {
        // console.log(res);
      }
    });
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function(e) {
    var img = this.data.imagePath;
    // console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  // 
  createQrCode: function(url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => {
      this.canvasToTempImage();
    }, 1000);

  },
  // createCodeImg
  createCodeImg: function() {
    var that = this;
    var url = this.data.url;
    // console.log(url);
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function() {
      wx.hideToast()
      var size = that.setCanvasSize();
      //绘制二维码
      that.createQrCode(url, "mycanvas", size.w, size.h);
      clearTimeout(st);
    }, 2000)
  }
})